// pages/loginChiceid/loginChiceid.js
// pages/assign_detail/assign_detail.js
var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chiceid: '',
    selected: 10,
    role: [],
    roleitems: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.loadsysRoles();
    var role = []
    var list = {
      roleName: "访客",
      elishRoleName: "Visitors",
      icon: '',
      bgstart: '#ff9f3c',
      bgend: '#ff9f3c',
      bg: 1,
    }

    role.push(list);
    var list = {
      roleName: "园区管理员",
      elishRoleName: "Park administrator ",
      icon: '',
      bgstart: '#2666FC',
      bgend: '#54B0F1',
      bg: 2,
    }
    role.push(list);
    var list = {
      roleName: "租户",
      elishRoleName: "Tenant",
      icon: '',
      bgstart: '#2666FC',
      bgend: '#54B0F1',
      bg: 2,
    }
    role.push(list);
    var list = {
      roleName: "业主",
      elishRoleName: "Owner",
      icon: '',
      bgstart: '#ff9f3c',
      bgend: '#ff9f3c',
      bg: 1,
    }
    role.push(list);

    this.data.role = role;
    // this.(this.data);
    this.setData({
      role: role
    })
  },
  chioce(e) {

    var index = e.currentTarget.dataset.index;

    var chiceid = e.currentTarget.dataset.id;

    this.setData({
      selected: index,
      chiceid: chiceid
    });

    switch (index) {
      case 0:

        break;
      case 0:

        break;

    }
  },
  //加载
  loadsysRoles() {

    var that = this;

    wx.request({
      url: applet_park_api_url + '/api/sysRoles',
      success(res) {
        
        if (res.data.code == 0) {
          let list = res.data.data;
          // 4*Math.floor(i/2)+i%2
          const yellowIndexs = [1, 4, 5, 8, 9, 12, 13, 16, 17, 20, 21, 24, 25];

          for (let i = 1; i <= list.length; i++) {
            if (yellowIndexs.indexOf(i) != -1) {
              list[i - 1].bg = '2';
            } else {
              list[i - 1].bg = '1'
            }
          }
          
          that.data.roleitems = list;
          that.setData(that.data);
        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })
  },
  submit() {
    var that = this;
    if (that.data.selected == 0) {
      wx.switchTab({
        url: '../my/my',
      })
      return
    }
    var id = wx.getStorageSync("id");
    if (!that.data.chiceid) {
      util.showModal('请选择校色');
      return
    }
    wx.request({
      url: applet_park_api_url + '/api/sysUsers/' + id,
      data: {
        currentRoleId: that.data.chiceid
      },
      method: 'PUT',
      success(res) {
        
        if (res.data.code == 0) {

          wx.switchTab({
            url: '../my/my',
          })
        } else {
          wx.showToast({
            title: res.data.message,
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */

  onUnload({
    next
  }) {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})